package shop.controller.front.cart;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.cart.CartService;
import shop.services.front.cart.bean.Cart;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuangjunfeng on 2017/6/3.
 */
@Controller
@RequestMapping("/front/Cart")
public class CartController  extends FrontBaseController<Cart> {
    @Resource
    private CartService cartService;
    @Override
    public Services<Cart> getService() {
        return cartService;
    }

    @RequestMapping(value = "/selectCartList",method = RequestMethod.GET)
    @ResponseBody
        public JSONResult selectCartList(Cart cart){
        JSONResult rs =new JSONResult();
        List<Cart> cartList=cartService.selectList(cart);
        rs.setData(cartList);
        rs.setSuccess(true);
        return rs;
    }
    @RequestMapping(value = "/updateCartList",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateCartList(Cart cart){
        JSONResult rs= new JSONResult();
        String[] ids=cart.getProductID().split(",");
        for (int i=0;i< ids.length;i++){
            cart.setProductID(ids[i]);
            cartService.delete(cart);
        }
        rs.setSuccess(true);
        return rs;
    }
    @RequestMapping(value = "/selectCartByIds",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectCartByIds(@RequestParam(value = "cartIDArr[]") String[] cartIDArr){
        JSONResult rs= new JSONResult();
        List<Cart> cartList=new ArrayList<>();//创建集合对象
        for (int i=0;i<cartIDArr.length;i++){
            cartList.add(getService().selectById(cartIDArr[i]));
        }
        rs.setData(cartList);
        rs.setSuccess(true);
        return rs;
    }
}
